//
//  MyTabBarController.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/20.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "MyTabBarController.h"
#import "DayNightManager.h"
#import "DataService.h"
#import "UIImageView+WebCache.h"

@interface MyTabBarController ()

@end

@implementation MyTabBarController {
    UIImageView* welcomePage1;
    UIImageView* welcomePage2;
    NSString* welcomeImage2UrlStr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //欢迎页面
    [self _loadWelcomePage1];
    //

    [self _createViewControllers];
    
    //接收通知，有人会告诉自己，切换切面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeIndexNotificationAction:) name:kTabBarControllerChangeIndexNotification object:nil];
}



- (void)changeIndexNotificationAction:(NSNotification*)notification {
    NSDictionary* info = notification.userInfo;
    NSInteger index = [info[@"index"] integerValue];
    
    [self setSelectedIndex:index];
}


//欢迎页面1
- (void)_loadWelcomePage1 {
    welcomePage1 = [[UIImageView alloc] initWithFrame:self.view.bounds];
    welcomePage1.image = [UIImage imageNamed:@"loading-4.7"];
    [self.view addSubview:welcomePage1];
    
    
    //网络请求,下载欢迎页面2
//    NSDictionary* params = @{@"strMs" : @0};
    [DataService oneRequestUrl:kWelcomePageUrl httpMethod:@"GET" params:nil block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"欢迎页面下载失败  %@", result);
            
            [self performSelector:@selector(_removePages) withObject:nil afterDelay:2];
            return;
        }
        
        NSString* str800480Url = result[@"str800480Url"];
//        str800480Url = @"http://pic.yupoo.com/hanapp/ER20FvC1/WcDUX.png";       //为模拟而生
        if (str800480Url.length == 0) {
            //当str800480Url为空时，就代表目前不需要这个欢迎页面
            [self performSelector:@selector(_removePages) withObject:nil afterDelay:2];
            return;
        }
        
        welcomeImage2UrlStr = str800480Url;
        [self performSelector:@selector(_loadWelcomePage2) withObject:nil afterDelay:2];
    }];
}

//欢迎页面2
- (void)_loadWelcomePage2 {
    welcomePage2 = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    welcomePage2.image = welcomeImage2;
    [welcomePage2 sd_setImageWithURL:[NSURL URLWithString:welcomeImage2UrlStr]];
    [self.view addSubview:welcomePage2];
    
    [self performSelector:@selector(_removePages) withObject:nil afterDelay:2];
    
//    [welcomePage1 removeFromSuperview];
}

- (void)_removePages {
    [welcomePage1 removeFromSuperview];
    [welcomePage2 removeFromSuperview];
}

- (void)_createViewControllers {
    UINavigationController* navHome = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] init]];
    UINavigationController* navArticle = [[UINavigationController alloc] initWithRootViewController:[[ArticleViewController alloc] init]];
    UINavigationController* navQuestion = [[UINavigationController alloc] initWithRootViewController:[[QuestionViewController alloc] init]];
    UINavigationController* navThing = [[UINavigationController alloc] initWithRootViewController:[[ThingViewController alloc] init]];
    UINavigationController* navUser = [[UINavigationController alloc] initWithRootViewController:[[UserViewController alloc] init]];
    
    NSArray* vcArray = @[navHome, navArticle, navQuestion, navThing, navUser];
    self.viewControllers = vcArray;
    
    
    
    navHome.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"homeSelected"]];
    navArticle.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"文章" image:[UIImage imageNamed:@"reading"] selectedImage:[UIImage imageNamed:@"readingSelected"]];
    navQuestion.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"问题" image:[UIImage imageNamed:@"question"] selectedImage:[UIImage imageNamed:@"questionSelected"]];
    navThing.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"东西" image:[UIImage imageNamed:@"thing"] selectedImage:[UIImage imageNamed:@"thingSelected"]];
    navUser.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"个人" image:[UIImage imageNamed:@"person"] selectedImage:[UIImage imageNamed:@"personSelected"]];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kTabBarControllerChangeIndexNotification object:nil];
}

@end
